//
//  DatePickerPopView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/6.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "DatePickerPopView.h"

@interface DatePickerPopView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIWindow * window;

@property(nonatomic,strong)UIButton * coverView;

@property(nonatomic,strong)UITableView * tableView;
@end

@implementation DatePickerPopView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        [self addRadius:5];
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = HexColor(@"#EEEEEE").CGColor;
        [self addSubview:self.tableView];
    }
    return self;
}
-(void)show{
    [self.window addSubview:self.coverView];
    [self.coverView addSubview:self];
}

-(void)hidden{
    [self.coverView removeFromSuperview];
    [self removeFromSuperview];
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 12;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.text = [NSString stringWithFormat:@"2016年%ld月",indexPath.row+1];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self hidden];
    if(self.delegate&&[self.delegate respondsToSelector:@selector(DatePickerPopView:didSelectedDate:)]){
        [self.delegate DatePickerPopView:self didSelectedDate:[NSString stringWithFormat:@"%ld",indexPath.row+1]];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DatePickerPopViewSelectedDate" object:nil userInfo:@{@"date":@(indexPath.row+1)}];
}

-(UIWindow*)window{

    if(_window == nil){
        _window = [[UIApplication sharedApplication].delegate window];
    }
    return _window;
}
-(UIButton*)coverView{
    if(_coverView == nil){
        _coverView = [[UIButton alloc] initWithFrame:self.window.bounds];
        [_coverView addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        _coverView.backgroundColor = [UIColor clearColor];
    }
    return _coverView;
}

-(UITableView*)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}
@end
